package com.dashu.authority.servie.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dashu.authority.common.Constant;
import com.dashu.authority.common.MessageCode;
import com.dashu.authority.entity.ButtonOperate;
import com.dashu.authority.exception.AuthorityException;
import com.dashu.authority.repository.ButtonOperateMapper;
import com.dashu.authority.servie.ButtonOperateService;
import com.dashu.authority.util.AuthorityServiceUtil;
import com.github.pagehelper.PageHelper;

@Service
public class ButtonOperateServiceImpl extends BaseServiceImpl<ButtonOperate> implements ButtonOperateService {
	
	@Autowired
	private ButtonOperateMapper buttonMapper;
	
	@Override
	public List<ButtonOperate> queryAllByExample(ButtonOperate entity, int pageNum, int pageSize)
			throws AuthorityException {
		PageHelper.startPage(pageNum, pageSize);
		Map<Object, Object> params = new HashMap<Object, Object>();
		if (entity!=null) {
			if (entity.getName()!=null && entity.getName()!="") {
				params.put("applicationName", entity.getName());
			}
			if (entity.getMenuId()!=null ) {
				params.put("menuId", entity.getMenuId());
			}
		}
		params.put("orderBy", "sore_num");
		params.put("sort", "ASC");
		return buttonMapper.queryAllByExample(params);
	}

	@Override
	public Map<String, Object> insertEntity(ButtonOperate entity) throws AuthorityException {
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("soreNum", entity.getSoreNum());
		params.put("menuId", entity.getMenuId());
		params.put("applicationId", entity.getApplicationId());
		if (entity.getSoreNum()!=null) {
			if (entity.getSoreNum()<0 ) {
				return AuthorityServiceUtil.OperaFailOtherMap(Constant.OPERATION_SAVE, "编号不能小于0！");
			}else if (entity.getSoreNum()>32) {
				return AuthorityServiceUtil.OperaFailOtherMap(Constant.OPERATION_SAVE, "编号不能大于32！");
			}
		}
		List<ButtonOperate> list = buttonMapper.queryAllByExample(params);
		if (list!=null && list.size()>0) {
			return AuthorityServiceUtil.OperaFailOtherMap(Constant.OPERATION_SAVE, "保存失败，编号不能重复！");
		}		
		
		int ret = buttonMapper.insertEntity(entity);
		if(ret>0){
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_SAVE);
		}else{
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_SAVE);
		}
	}

	@Override
	public Map<String, Object> updateEntity(ButtonOperate entity) throws AuthorityException {
		int ret = buttonMapper.updateEntity(entity);
		if(ret>0){
			System.out.println(AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_SAVE));
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_SAVE);
		}else{
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_SAVE);
		}
	}

	@Override
	public Map<String, Object> deleteByIds(Integer[] ids) throws AuthorityException {
		if(ids==null || ids.length==0){
		//	return AuthorityServiceUtil.OperaFailOtherMap(Constant.OPERATION_SAVE, "保存失败，编号不能重复！");
			throw new AuthorityException(MessageCode.ERROR_PARAMS_NULL.toString(),"信息id数组:");
		}
		Map<Object,Object> params=new HashMap<Object,Object>();
		params.put("ids", ids);
		int ret=buttonMapper.deleteByIds(params);
		if(ret>0){
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_DELETE);
		}else {
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_DELETE);
		}
	}
	
	
	@Override
	public List<ButtonOperate> queryAllByMenuIdAndIds(Integer menuId,List<Integer> ids , int pageNum, int pageSize)
			throws AuthorityException {
		AuthorityServiceUtil.checkParam("菜单id", menuId);
		AuthorityServiceUtil.checkParam("菜单id", ids);
		PageHelper.startPage(pageNum, pageSize);
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("menuId", menuId);
		if (ids!=null&&ids.size()>0) {
			params.put("ids", ids);
		}
		params.put("orderBy", "create_time");
		params.put("sort", "desc");
		return buttonMapper.queryAllByExample(params);
	}

}
